<template>
    <div>

        <div style="margin: 10px 0">
            <el-input style="width: 200px" placeholder="请输入船舶订单号" suffix-icon="el-icon-search"
                      v-model="shipOrder"></el-input>
            <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
            <el-button type="warning" @click="reset">重置</el-button>
        </div>

        <div style="margin: 10px 0">
            <template>
                <el-popconfirm
                        class="ml-5"
                        confirm-button-text='确定'
                        cancel-button-text='我再想想'
                        icon="el-icon-info"
                        icon-color="red"
                        title="确定删除吗？"
                        @confirm="delBatch"
                >
                    <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
                </el-popconfirm>
            </template>
        </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
                  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="ID" width="80"></el-table-column>
            <el-table-column prop="truckOrder" label="集卡订单"></el-table-column>
            <el-table-column prop="shipOrder" label="船舶订单"></el-table-column>
            <el-table-column prop="userId" label="用户id"></el-table-column>
            <el-table-column prop="yard" label="场区号"></el-table-column>
            <el-table-column prop="row" label="贝号"></el-table-column>
            <el-table-column prop="column" label="列号"></el-table-column>
            <el-table-column prop="floor" label="层号"></el-table-column>
            <el-table-column label="操作" width="300" align="center">
                <template slot-scope="scope">
                    <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
                </template>
            </el-table-column>
        </el-table>
        <div style="padding: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[5, 10, 20, 50]"
                    :page-size="5"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>
        <el-dialog title="订单信息" :visible.sync="dialogFormVisible" width="30%">
            <el-form label-width="120px">
                <el-form-item label="集卡订单编号" style="width: 71%">
                    <el-input v-model="form.truckOrder" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="save">确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {serverIp, serverPort} from "../config/config";

    export default {
        name: 'ShipOrder',
        data() {
            return {
                user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
                serverIp: serverIp,
                serverPort: serverPort,
                tableData: [],
                total: 0,
                pageNum: 1,
                pageSize: 5,
                shipOrder: "",
                form: {},
                multipleSelection: [],
                dialogFormVisible: false,
                username: JSON.parse(localStorage.getItem("user")).username
            }
        },
        created() {
            this.load()
        },
        methods: {
            load() {
                if (this.user.role != 'ROLE_USER' || this.shipOrder != "") {
                    this.request.get("/pickUp/page", {
                        params: {
                            pageNum: this.pageNum,
                            pageSize: this.pageSize,
                            shipOrder: this.shipOrder,
                        }
                    }).then(res => {
                        this.tableData = res.data.data
                        this.total = res.data.total
                    })
                }
            },
            reset() {
                this.shipOrder = ""
                this.load()
            },
            handleSizeChange(pageSize) {
                this.pageSize = pageSize;
                this.load()
            },
            handleCurrentChange(pageNum) {
                this.pageNum = pageNum;
                this.load()
            },
            handleSelectionChange(val) {
                this.multipleSelection = val
            },
            handleAdd() {
                this.form = {}
                this.dialogFormVisible = true
            },
            save() {
                this.request.post("/pickUp", this.form).then(res => {
                    if (res.code === '200') {
                        this.$message.success("保存成功")
                        this.dialogFormVisible = false
                    } else {
                        this.$message.error("保存失败")
                    }
                })
                this.request.get("/pickUp/user", {
                    params: {
                        id: this.form.id,
                        username: this.username
                    }
                }).then(res => {
                    if (res.code === '200') {
                        this.$message.success("保存成功")
                        this.load()
                    } else {
                        this.$message.error("保存失败")
                    }
                })
            },
            handleEdit(row) {
                this.form = row
                this.dialogFormVisible = true
            },
            del(id) {
                this.request.get("/pickUp/delete?id=" + id).then(res => {
                    if (res.data) {
                        this.$message.success("删除成功")
                        this.load()
                    } else {
                        this.$message.error("删除失败")
                    }
                })
            },
            delBatch() {
                let ids = this.multipleSelection.map(v => v.id)
                this.request.post("/pickUp/delete/batch", ids).then(res => {
                    if (res.data) {
                        this.$message.success("删除成功")
                        this.load()
                    } else {
                        this.$message.error("删除失败")
                    }
                })
            }
        }
    }
</script>

<style>
    .headerBg {
        background: #eee !important;
    }
</style>
